﻿if (top != self) {
    if (top.location != self.location)
        top.location = self.location;
}

$(function () {
    $('#btnSubmit').click(function () {
        login();
    });

    document.onkeyup = function (e) {
        var code = e.charCode || e.keyCode;
        if (code == 13) {
            $('#btnSubmit').click();
        }
    }
})

var login = function () {
    var formdata = $('#loginForm').serializeObject();      //序列化
    var isCode = $('#isCode').val();
    if (isCode == "1") {
        var token = $('input[name="Token"]').val();
        if (token == "") {
            dialog.warning("请拖动滑块完成拼图！");
            return;
        }
    }
    $('#btnSubmit').addClass('layui-btn-disabled');

    layui.use(['form', 'element', 'jquery', 'button', 'popup'], function () {
        var form = layui.form;
        var element = layui.element;
        var button = layui.button;
        var popup = layui.popup;
        var $ = layui.jquery;

        $.ajax({
            type: "post",
            url: Root + 'Login/Logon',
            data: { model: formdata },
            success: function (data) {
                if (data.SubmitResult) {
                    popup.success("登录成功", function () {
                        location.href = Root + "home/index";
                    });
                    //location.href = PSIRoot + "home/index";
                } else {
                    dialog.warning(data.Message);
                    captcha.reload();
                }
            },
            error: function (result) {
                console.log(result);
                dialog.danger('操作失败！请重试或者联系管理员');
                captcha.reload();
            },
            complete: function () {
                $('input[name="Token"]').val('');
                $('#btnSubmit').removeClass('layui-btn-disabled');
            }
        });

    });
}